﻿//-----------------------------------------------------------------------
// <copyright file="MLFieldNameValue.cs" company="MAPILab Ltd">
//     Copyright (c) MAPILab Ltd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace MAPILab.SharePoint.Explorer.ObjectBrowser.TreeExpand
{
    using System;
    using Microsoft.SharePoint;

    /// <summary>
    /// Special class for SPField for object browser tree tag
    /// </summary>
    class MLFieldNameValue
    {
        #region Fields
        
        /// <summary>
        /// SharePoint field
        /// </summary>
        SPField spField;

        /// <summary>
        /// Raw value
        /// </summary>
        object rawValue;

        /// <summary>
        /// SharePoint field value
        /// </summary>
        object spFieldValue;
        
        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets SharePoint field
        /// </summary>
        public SPField Field
        {
            get { return spField; }
        }

        /// <summary>
        /// Gets row value
        /// </summary>
        public object RawValue
        {
            get { return rawValue; }
        }

        /// <summary>
        /// Gets SharePoint field value
        /// </summary>
        public object Value
        {
            get { return spFieldValue; }
        }

        #endregion Properties

        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="field"></param>
        /// <param name="rawValue"></param>
        public MLFieldNameValue(SPField field, object rawValue)
        {
            spField = field;
            this.rawValue = rawValue;

            try
            {
                if (this.rawValue != null)
                {
                    spFieldValue = spField.GetFieldValue(this.rawValue.ToString());
                }
            }
            catch (System.Exception e)
            {
                spFieldValue = e;
            }
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Override ToString Method
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string strRow = "null";
            if (RawValue != null)
            {
                strRow = RawValue.ToString();
            }

            string strVal = "null";
            if (Value != null)
            {
                if (Value is Exception)
                {
                    strVal = "<error>";
                }
                else
                {
                    strVal = Value.ToString();
                }
            }

            return string.Format("{0}={1};[{2}]", spField.ToString(), strVal, strRow);
        }

        #endregion Methods
    }
}
